//
//  ViewController.m
//  test
//
//  Created by heew on 15/10/27.
//  Copyright © 2015年 heew. All rights reserved.
//

#import "ViewController.h"


@interface Engine : NSObject <NSCoding>
@property (assign, nonatomic) NSString *horsePower; /**马力 */
//- (void)encodeWithCoder:(NSCoder *)aCoder ;
//- (instancetype)initWithCoder:(NSCoder *)aDecoder;
@end

@implementation Engine
//- (void)encodeWithCoder:(NSCoder *)aCoder {
//    [aCoder encodeInteger:self.horsePower forKey:@"horsePower"];
//}
//- (instancetype)initWithCoder:(NSCoder *)aDecoder {
//   self.horsePower = [aDecoder decodeIntegerForKey:@"horserPower"];
//    return self;
//}
@end

@interface Car : NSObject
@property (strong, nonatomic) NSString *name; /**名称 */
@property (strong, nonatomic) Engine *engine; /**发动机 */
@end
@implementation Car

@end


@interface ViewController () <UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIDatePicker *datePikcerView;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIButton *阿斯蒂芬;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *sdf;
@property (strong, nonatomic) NSArray *pickers; /**<#属性名#> */
@end

@implementation ViewController
- (IBAction)backGroundTap:(UIControl *)sender {
    NSLog(@"点击了屏幕");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *str = @"\u6587\u4ef6\u6570\u636e\u4e3a\u7a7a";
    NSData *date = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [[NSString alloc] initWithData:date encoding:NSUTF8StringEncoding];
    NSLog(@"%@",string);
}

- (void)predicateTest {
    
    Car *car = [Car new];
    Car *car1 = [Car new];
    Car *car2 = [Car new];
    Car *car3 = [Car new];
    Car *car4 = [Car new];
    Car *car5 = [Car new];
    car.name = @"Herbie";
    Engine *engine = [Engine new];
    car.engine = engine;
    car.engine.horsePower = @"160";
    car1.name = @"benchi";
    Engine *engine1 = [Engine new];
    engine1.horsePower = @"150";
    car1.engine = engine1;
    car2.name = @"Snugs";
    Engine *engine2 = [Engine new];
    car2.engine = engine2;
    car2.engine.horsePower = @"170";
    car3.name = @"Badger";
    Engine *engine3 = [Engine new];
    car3.engine = engine3;
    car3.engine.horsePower = @"130";
    car4.name = @"Flap";
    Engine *engine4 = [Engine new];
    car4.engine = engine4;
    car4.engine.horsePower = @"110";
    car5.name = @"BMW";
    Engine *engine5 = [Engine new];
    car5.engine = engine5;
    car5.engine.horsePower = @"190";
    NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
    NSPredicate *predicate;
    NSDictionary *varDict;
    varDict = [NSDictionary dictionaryWithObjectsAndKeys:
               @"Herbie", @"NAME", nil];
    predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
    NSLog(@"SNORGLE: %@", predicate);
    BOOL match = [predicate evaluateWithObject: car];
    NSLog (@"%s", (match) ? "YES" : "NO");
    
    predicate = [NSPredicate predicateWithFormat: @"SELF.name != %@",@"benchi"];
    NSMutableArray *cars = [NSMutableArray arrayWithArray:@[car,car1,car2,car3,car4,car5]];
    //    NSLog(@"%@",predicate);
    //    for (Car *car in cars) {
    //        if ([predicate evaluateWithObject: car]) {
    //            NSLog (@"%@", car.name);
    //        }
    //    }
    //    NSArray *engines = @[engine,engine2,engine3,engine4,engine5];
    [cars filterUsingPredicate:predicate];
    NSLog(@"%zd",cars.count);}

- (void)test1 {
    NSString *string = @"83924.skdjf";
    NSString *intergerString = [NSString stringWithFormat:@"%ld",[string integerValue]];
    NSLog(@"%lu",(unsigned long)intergerString.length);
    
    NSString *stringAfterPoint = nil;
    NSRange rangeOfPoint = [string rangeOfString:@"."];
    if (rangeOfPoint.location < string.length) {
        stringAfterPoint = [string substringFromIndex:rangeOfPoint.location + 1];
    }
    NSLog(@"%lu",stringAfterPoint.length);
    
}

- (void)test {
    NSDate* minDate = [NSDate date];
    NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:7 * 24 * 60 * 60];
    self.datePikcerView.minuteInterval = 30;
    self.datePikcerView.minimumDate = minDate;
    self.datePikcerView.maximumDate = maxDate;
    
    //    NSDateComponents *cps = [[NSDateComponents alloc] init];
    //    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    //    NSCalendar *calendar = [NSCalendar currentCalendar];
    ////    NSDate *date =  [calendar startOfDayForDate:[NSDate dateWithTimeIntervalSince1970:12983798]];
    ////    NSLog(@"%@",date);nsarar
    //    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:7];
    //    for (int i = 0; i < 10; i++) {
    //        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(i + 1) * 24 * 60 * 60];
    //        NSLog(@"%@",[calendar components:NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:date]);
    ////        NSLog(@"%ld",[calendar component:NSCalendarUnitYear fromDate:date]);
    //        [arrayM addObject:date];
    //    }
    
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"请输入材料数目与价格" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    UITextField *textField1 = [alert textFieldAtIndex:0];
    textField1.text = [NSString stringWithFormat:@"%d",1876876];
    textField1.placeholder = @"请输入材料数目";
    textField1.clearButtonMode =UITextFieldViewModeWhileEditing;
    textField1.returnKeyType = UIReturnKeyDone;
    
    UITextField *textField = [alert textFieldAtIndex:1];
    textField.text = [NSString stringWithFormat:@"%.1f",1.222];
    textField.placeholder = @"请输入价格";
    textField.clearButtonMode =UITextFieldViewModeWhileEditing;
    textField.returnKeyType = UIReturnKeyDone;
    textField.secureTextEntry = NO;
    alert.delegate = self;
    [alert show];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cms = [[NSDateComponents alloc] init];
    cms.year = 2015;
    cms.month = 10;
    cms.day = 31;
    cms.hour = 8;
    cms.minute = 10;
    NSDate *bookDate = [calendar dateFromComponents:cms];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat =  @"yyyy-MM-dd HH-mm";
    
    NSTimeInterval time = [bookDate timeIntervalSince1970];
    for (int i = 0; i < 7; i++) {
        time += (i + 1) * 24 * 60 * 60;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
        NSLog(@"%@",date);
    }
    /*
     
     - (NSRange)minimumRangeOfUnit:(NSCalendarUnit)unit;
     - (NSRange)maximumRangeOfUnit:(NSCalendarUnit)unit;
     
     - (NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date;
     - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date;
     
     - (BOOL)rangeOfUnit:(NSCalendarUnit)unit startDate:(NSDate * __nullable * __nullable)datep interval:(nullable NSTimeInterval *)tip forDate:(NSDate *)date NS_AVAILABLE(10_5, 2_0);
     
     - (nullable NSDate *)dateFromComponents:(NSDateComponents *)comps;
     - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date;
     
     - (nullable NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSCalendarOptions)opts;
     
     - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts;
     
     
     This API is a convenience for getting era, year, month, and day of a given date.
     Pass NULL for a NSInteger pointer parameter if you don't care about that value.
     */
    
    
    //        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请选择支付方式" message:@"请填写次数" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"在线支付",@"现金支付", nil];
    //        [alert setAlertViewStyle:UIAlertViewStyleDefault];
    //        [alert show];
    
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择支付方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"在线支付",@"现金支付", nil];
    [sheet showInView:self.view];
    
    self.pickers = @[
                     @[@"11月"],
                     @[@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日"],
                     @[@"7:00",@"8:00",@"9:00",@"10:00",@"11:00",@"12:00",@"14:00",@"15:00",@"16:00",@"17:00"],
                     @[@"00",@"30"]
                     ];
    
    
    
    //文件管理
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //缓存路径
    NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
    NSString *cacheDir = [cachePaths objectAtIndex:0];
    NSArray *cacheFileList;
    NSEnumerator *cacheEnumerator;
    NSString *cacheFilePath;
    unsigned long long cacheFolderSize = 0;
    cacheFileList = [fileManager subpathsOfDirectoryAtPath:cacheDir error:nil];
    cacheEnumerator = [cacheFileList objectEnumerator];
    
    while (cacheFilePath = [cacheEnumerator nextObject])
    {
        NSDictionary *cacheFileAttributes = [fileManager attributesOfItemAtPath:[cacheDir stringByAppendingPathComponent:cacheFilePath] error:nil];
        cacheFolderSize += [cacheFileAttributes fileSize];
    }
    //单位MB
    NSLog(@"%llu",cacheFolderSize/1024);
}

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.pickers.count;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.pickers[component] count];
}


- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSArray *componentA =(NSArray *)self.pickers[component];
    return (NSString *)componentA[row];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //    [self.indexView setIndexes:@[@"wejflksjd",@"sdhfd",@"iuhdiuhfwefh"]];
    //    NSJSONSerialization *json = [[NSJSONSerialization alloc] init];
    //    json
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
